<?php

namespace app\webapi\controller;

use app\webapi\common\BaseController;
use app\webapi\vms\service\SlideService;
use app\webapi\vms\validate\SlideValidate;
use library\OWException;
use think\App;

/**
 * @幻灯片控制器
 */
class SlideController extends BaseController
{
    /**
     * @var SlideValidate
     */
    protected $validate;

    /**
     * @var SlideService
     */
    protected $service;

    /**
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->validate = new SlideValidate();
        $this->service = new SlideService();
    }

    /**
     * @desc 获取幻灯片列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/12-17:00
     */
    public function getList()
    {
        $params = $this->validate->goCheck('getList');
        $res = $this->service->getList($params['page_no'], $params['page_size'], $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 获取幻灯片详情
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/12-17:00
     */
    public function getInfo()
    {
        $params = $this->validate->goCheck('getInfo');
        $res = $this->service->getInfo($params['id'], $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 编辑幻灯片
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/12-17:07
     */
    public function edit()
    {
        $params = $this->validate->goCheck('edit');
        $res = $this->service->edit($params, $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 删除幻灯片
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/12-17:28
     */
    public function delete()
    {
        $params = $this->validate->goCheck('delete');
        $id_arr = explode(',', $params['ids']);
        foreach ($id_arr as $v) {
            if (!is_numeric($v)) {
                throw (new OWException('参数格式错误'));
            }
        }
        $res = $this->service->delete($id_arr, $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 幻灯片排序
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/12-17:30
     */
    public function sort()
    {
        $params = $this->validate->goCheck('sort');
        $json_array = $this->validate->check_sort_json_array($params['json_array'], ['id' => 'number', 'sort' => 'number']);
        $res = $this->service->sort($json_array, $this->website_id);
        return $this->responseSuccess($res);
    }
}